//
//  GS_DubDetail_ViewController.m
//  GoodManNews
//
//  Created by dllo on 16/1/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "GS_DubDetail_ViewController.h"
#import "GoodManNews_AFNetTool.h"
#import "GS_DubTableViewCell.h"
#import <STKAudioPlayer.h>

@interface GS_DubDetail_ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *dubArr;
@property (nonatomic, retain) STKAudioPlayer *player;



@end

@implementation GS_DubDetail_ViewController

- (void)dealloc
{
    [_tableV release];
    [_player release];
    [_heroName release];
    [_dubArr release];
    [super dealloc];
}

+(instancetype)sharePlayerViewController
{
    static GS_DubDetail_ViewController *playerVC = nil;
    if (nil == playerVC) {
        playerVC = [[GS_DubDetail_ViewController alloc] init];
    }
    return playerVC;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    self.tableV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [_tableV release];
    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.view);
        
    }];
    STKAudioPlayerOptions playerOptions = {YES, YES, {50, 100, 200, 400, 800, 1600, 2600, 16000}};
    self.player = [[STKAudioPlayer alloc] initWithOptions:playerOptions];
    self.player.volume = 3;
    
}

- (void)getData {
//    NSLog(@"+++++%@", self.heroName);
    NSString *url = [NSString stringWithFormat:@"http://box.dwstatic.com/apiHeroSound.php?hero=%@", self.heroName];
    [GoodManNews_AFNetTool getNetWithURL:url body:nil headFile:nil responseStyle:WBNJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        self.dubArr = [NSMutableArray array];
        for (NSString *str in responseObject) {
            [self.dubArr addObject:str];
        }
        
        [self.tableV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}

- (void)getNum:(NSInteger)num
{
//    NSLog(@"%ld", num);
    if (3 != num) {
        [self.player stop];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    if (nil != self.dubArr) {
        return self.dubArr.count;
    } else {
        return 0;
    }
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    NSString *str = @"cell";
    GS_DubTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (nil == cell) {
        cell = [[GS_DubTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
        cell.Label.text = self.dubArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = [NSString stringWithFormat:@"http://box.dwstatic.com/sounds/%@/%@", self.heroName, self.dubArr[indexPath.row]];
//    NSLog(@"%@", str);
    [self.player play:[str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.player stop];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
